<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  size="small"  :inline="true" label-width="68px" v-if="chartInfo && chartInfo.search_type" >
      <el-form-item :label="chartInfo.search_title" prop="searchVal">
        <treeselect  v-if="chartInfo.search_type == 'tree'" 
          v-model="queryParams.searchVal"
          :options="searchOptions"
          style="width: 180px"
          :normalizer="normalizer"
          placeholder="请选择"
        />
        <el-select v-if="chartInfo.search_type == 'ds'"
          v-model="queryParams.searchVal" placeholder="请选择" clearable >
          <el-option
            v-for="dict in searchOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-select v-if="chartInfo.search_type == 'dict'"
          v-model="queryParams.searchVal" placeholder="请选择" clearable >
          <el-option
            v-for="dict in dict.type[charInfo.search_ds]"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>

        <el-date-picker v-if="chartInfo.search_type == 'dt'"
          clearable
          v-model="queryParams.searchVal"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择时间范围"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary"  icon="el-icon-search" size="mini" @click="handleQuery">确定</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div ref="chartDiv" :class="className" :style="{ height: height, width: width }" />
  </div>
</template>

<script>
import { apiGet, apiDs } from "@/api/openapi";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./options/resize";
import lineChart from "./options/line";
import barChart from "./options/bar";
import pieChart from "./options/pie";
import radarChart from "./options/radar";
import guageChart from "./options/guage";

export default {
  name: "ChartView",
  dicts: [],
  components: {
    Treeselect,
  },
  mixins: [resize],
  data() {
    return {
      chartId: null,
      chartInfo: null,
      queryParams: {
        searchVal: null,
      },
      searchOptions: [],
      chartData: null,
      chart: null,
      className: "chart",
      width: "100%",
      height: "600px",
      autoResize: true,
    };
  },
  created() {
    const chartId = this.$route.query && this.$route.query.id;
    apiGet({
      "@table": "dev_chart",
      id: chartId,
    }).then((response) => {
      this.chartInfo = response.data;

      //加载数据
      this.getChartData();

      //生成搜索条件
      if (!this.chartInfo.search_type) {
        return;
      }

      if (this.chartInfo.search_type == "dt") {
        this.queryParams.searchVal = [];
      } else if (this.chartInfo.search_type == "dict") {
        this.dict.init(this.chartInfo.search_ds);
      } else {
        apiDs(this.chartInfo.search_ds).then((response) => {
          if (this.chartInfo.search_type == "tree") {
            this.searchOptions = this.handleTree(response.data, "value", "pid");
          } else {
            this.searchOptions = response.data;
          }
        });
      }
    });
    this.chartId = chartId;
  },
  mounted() {
    this.$nextTick(() => {
      this.chart = echarts.init(this.$refs.chartDiv, "macarons");
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    getChartData() {
      let params = {};
      if (this.chartInfo.search_type && this.queryParams.searchVal != null) {
        params[this.chartInfo.search_field] = this.queryParams.searchVal;
      }
      apiDs(this.chartInfo.ds_code, params).then((response) => {
        this.parseChartData(response.data);
      });
    },
    parseChartData(dsData) {
      let script = this.chartInfo.data_option;
      let set = null;
      if(script){
        //自定义方法解析
        if (script.indexOf("function") >= 0 && script.indexOf("exec") >= 0 ) {
          script += ";\r\nexec(" + JSON.stringify(dsData) + ");";
          console.log("eval: " + script);
          this.chartData = eval(script);
          console.log(this.chartData);
          this.chart.setOption(this.chartData);
          return;
        }else{
          //个性化配置
          set = JSON.parse(script);
        }
      }
      
      if ("pie" == this.chartInfo.type) {
        this.chartData = pieChart.setData(dsData, set);
      } else if ("line" == this.chartInfo.type) {
        this.chartData = lineChart.setData(dsData, set);
      } else if ("bar" == this.chartInfo.type) {
        this.chartData = barChart.setData(dsData, set);
      } else if ("radar" == this.chartInfo.type) {
        this.chartData = radarChart.setData(dsData, set);
      } else if ("guage" == this.chartInfo.type) {
        this.chartData = guageChart.setData(dsData, set);
      } else {
        this.$modal.msgError("配置错误");
        return;
      }
      console.log(this.chartData);
      this.chart.setOption(this.chartData);
    },
    /** 转换资产分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.value,
        label: node.label,
        children: node.children,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getChartData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      if (this.chartInfo.search_type && this.chartInfo.search_type == "dt") {
        this.queryParams.searchVal = [];
      } else {
        this.queryParams.searchVal = null;
      }

      this.getChartData();
    },
  },
};
</script>
